package com.kg.module.jtnc.zzjh.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kg.core.annotation.AutoOperateLog;
import com.kg.core.annotation.NoRepeatSubmit;
import com.kg.core.exception.BaseException;
import com.kg.core.web.ResponseResult;
import com.kg.module.jtnc.dkgl.entity.KrDkgl;
import com.kg.module.jtnc.zzjh.dto.KrZzjhDTO;
import com.kg.module.jtnc.zzjh.dto.convert.KrZzjhConvert;
import com.kg.module.jtnc.zzjh.entity.KrZzjh;
import com.kg.module.jtnc.zzjh.service.KrZzjhService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 种植计划表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-02
 */
@RestController
@RequestMapping("/jtnc/zzjh/krZzjh")
@Api(tags = "种植计划表", value = "种植计划表", description = "KrZzjhController")
public class KrZzjhController {

    @Resource
    private KrZzjhService krZzjhService;
    @Resource
    private KrZzjhConvert krZzjhConvert;


    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/enable", value = "种植计划启用", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "zzjhId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/enable")
    public void enable(String zzjhId) {
        KrZzjh byId = krZzjhService.getById(zzjhId);
        krZzjhService.lambdaUpdate().set(KrZzjh::getState, 0)
                .eq(KrZzjh::getDkglId, byId.getDkglId())
                .eq(KrZzjh::getState, 1)
                .update();
        krZzjhService.lambdaUpdate().set(KrZzjh::getState, 1)
                .eq(KrZzjh::getZzjhId, zzjhId)
                .update();
    }
    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/disable", value = "种植计划禁用", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "zzjhId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/disable")
    public void disable(String zzjhId) {
        krZzjhService.lambdaUpdate().set(KrZzjh::getState, 0)
                .eq(KrZzjh::getZzjhId, zzjhId)
                .update();
    }


    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/getZzjhByDkId", value = "根据地块查询当前启用的计划信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dkglId", value = "地块ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getZzjhByDkId")
    public KrZzjh getZzjhByDkId(String dkglId) throws BaseException {
        KrZzjh one = krZzjhService.lambdaQuery().eq(KrZzjh::getDkglId, dkglId)
                .eq(KrZzjh::getState, 1)
                .last("limit 1")
                .one();
        if (BeanUtil.isEmpty(one)){
            throw new BaseException("当前地块没有启用种植计划！");
        }
        return one;
    }


    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/getById", value = "详情-种植计划表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "zzjhId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getById")
    @PreAuthorize("hasAuthority('jtnc:zzjh:krZzjh:getById')")
    public KrZzjhDTO getById(String zzjhId) {
        return krZzjhConvert.entityToDto(krZzjhService.getById(zzjhId));
    }

    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/list", value = "分页列表-种植计划表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('jtnc:zzjh:krZzjh:list')")
    public Page<KrZzjhDTO> list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                @RequestParam(value = "params", required = false) String params) {
        return krZzjhService.pagelist(page, limit, params);
    }

    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/add", value = "新增-种植计划表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('jtnc:zzjh:krZzjh:add')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/zzjh/krZzjh/add", logMsg = "新增-种植计划表")
    public void add(@RequestBody KrZzjhDTO krZzjhDTO) throws BaseException {
        try {
            krZzjhService.add(krZzjhDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "新增失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/update", value = "修改-种植计划表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('jtnc:zzjh:krZzjh:update')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/zzjh/krZzjh/update", logMsg = "修改-种植计划表")
    public void update(@RequestBody KrZzjhDTO krZzjhDTO) throws BaseException {
        try {
            krZzjhService.update(krZzjhDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "修改失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/delete", value = "删除-种植计划表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "zzjhIds", value = "待删除id列表", paramType = "body", required = true, allowMultiple = true, dataType = "String")
    })
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('jtnc:zzjh:krZzjh:delete')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/zzjh/krZzjh/delete", logMsg = "删除-种植计划表")
    public void delete(@RequestBody String[] zzjhIds) throws BaseException {
        try {
            krZzjhService.delete(Arrays.asList(zzjhIds));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "删除失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/export/excel", value = "导出excel-种植计划表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/export/excel")
    @PreAuthorize("hasAuthority('jtnc:zzjh:krZzjh:export:excel')")
    public String exportExcel(@RequestParam(value = "params", required = false) String params) throws BaseException {
        String result = krZzjhService.exportExcel(params);
        if ("error".equals(result)) {
            throw new BaseException("导出Excel失败，请重试！");
        }
        return result;
    }

    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/import/excel", value = "导入excel-种植计划表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", paramType = "query", required = false, dataType = "HttpServletRequest")
    })
    @PostMapping("/import/excel")
    @PreAuthorize("hasAuthority('jtnc:zzjh:krZzjh:import:excel')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/zzjh/krZzjh/import/excel", logMsg = "导入excel-种植计划表")
    public ResponseResult importExcel(HttpServletRequest request) throws BaseException {
        try {
            String result = krZzjhService.importExcel(request);
            if (StringUtils.hasText(result)) {
                // 导入失败，返回错误提示信息
                return ResponseResult.builder().code("200").message(result).build();
            } else {
                return ResponseResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "导入Excel失败，请重试！");
        }
    }

    @ApiOperation(notes = "/jtnc/zzjh/krZzjh/import/downloadTemplate", value = "下载导入模板-种植计划表", httpMethod = "GET")
    @GetMapping("/import/downloadTemplate")
    public String downloadTemplate() throws BaseException {
        String result = krZzjhService.downloadTemplate();
        if ("error".equals(result)) {
            throw new BaseException("下载导入模板失败，请重试！");
        }
        return result;
    }
}
